/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2021/5/21
 * Time: 22:51
 */

#include "CustomChart.h"
CustomChart::CustomChart(QGraphicsItem * parent, const Qt::WindowFlags & wFlags)
    : QChart(QChart::ChartTypeCartesian, parent, wFlags)
    , current_series_(nullptr)
    , axisX_(new QBarCategoryAxis())
    , axisY_(new QValueAxis())
{
    addAxis(axisY_, Qt::AlignLeft);
    addAxis(axisX_, Qt::AlignBottom);
}

void CustomChart::HandleBarClicked(int index, QBarSet * barset)
{
    Q_UNUSED(barset)
    auto * customSeries = dynamic_cast<CustomSeries *>(sender());
    this->ChangeSeries(customSeries->OwnSeries(index));
}
void CustomChart::ChangeSeries(CustomSeries * series)
{
    if(nullptr != current_series_)
        this->removeSeries(current_series_);

    current_series_ = series;
    axisX_->setCategories(current_series_->Categories());
    this->addSeries(current_series_);
    series->attachAxis(axisX_);
    series->attachAxis(axisY_);

    axisY_->setRange(0, current_series_->GetMaxValue());

    setTitle(series->name());
}
